//食物对象
var Food = {
        //食物位置
        food: null,
        //创建食物的方法
        CreatFood: function() {
            do {
                this.food = Math.floor(Math.random() * 399);
            } while (Snack.snack.indexOf(this.food) != -1);
            $("li").eq(this.food).addClass("food");
        }
    }
    //Snack对象
var Snack = {
        //蛇体
        snack: [],
        //蛇的行走方向
        direction: 4, //top:1,bottom:2,left:3,right:4
        //蛇移动时的计时器
        timeId: null,
        //蛇移动的速度，默认为500毫秒
        speed: 300,
        //是否暂停
        pause: true, //true为暂停,false为游戏中
        //蛇的移动方法
        snackMove: function() {
            if (!this.pause) { //检测游戏状态，如果是暂停中，蛇布不能移动
                return;
            }
            var step = 1; //蛇头移动的步数
            clearInterval(this.timeId);
            var that = this;
            this.timeId = setInterval(function() {
                switch (that.direction) {
                    case "":
                        step = 0;
                        break;
                    case 1:
                        step = -20;
                        break;
                    case 2:
                        step = 20;
                        break;
                    case 3:
                        step = -1;
                        break;
                    case 4:
                        step = 1;
                        break;
                }
                //重新计算吃东西或者没吃东西小蛇的身体
                that.eatFood(that, step);
                //判断小蛇是否死亡
                if (!that.IsDied(that)) {
                    clearInterval(that.timeId);
                    alert("小蛇蛇，扑街！");
                    return;
                };
                //将小蛇渲染到game-box中
                $("li").removeClass("snack");
                for (var i = 0; i < that.snack.length; i++) {
                    $("li").eq(that.snack[i]).addClass("snack");
                }
            }, this.speed);
        },
        //死亡判断函数
        IsDied: function(that) {
            //蛇头
            var snackHead = that.snack[that.snack.length - 1];
            //蛇脖子
            var snackNeck = that.snack[that.snack.length - 2];
            if ((that.snack.indexOf(snackHead) != -1 && that.snack.indexOf(snackHead) != (that.snack.length - 1))) {
                return false;
            } else if ((GameBox.topSide.indexOf(snackNeck) != -1) && that.direction == 1) {
                return false;
            } else if (GameBox.bottomSide.indexOf(snackNeck) != -1 && that.direction == 2) {
                return false;
            } else if (GameBox.leftSide.indexOf(snackNeck) != -1 && that.direction == 3) {
                return false;
            } else if (GameBox.rightSide.indexOf(snackNeck) != -1 && that.direction == 4) {
                return false;
            } else {
                return true;
            }
        },
        //蛇吃东西方法，并重新计算蛇的身体
        eatFood: function(that, step) {
            if (that.snack[that.snack.length - 1] + step == Food.food) {
                //食物被吃变成蛇的身体，颜色改变
                $("li.food").removeClass("food").addClass("snack");
                //吃下东西后蛇身体变长
                that.snack.push(that.snack[that.snack.length - 1] + step);
                //重新创建食物
                Food.CreatFood();
            } else {
                that.snack.push(that.snack[that.snack.length - 1] + step);
                that.snack.shift();
            }
        },
        // 初始化小蛇
        CreatSnack: function() {
            this.snack = [251, 252, 253];
            $("li").removeClass("snack");
            for (var i = 0; i < this.snack.length; i++) {
                $("li").eq(this.snack[i]).addClass("snack");
            }
        }
    }
    //GameBox对象
var GameBox = {
    //四条边界
    topSide: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    leftSide: [0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380],
    rightSide: [19, 39, 59, 79, 99, 119, 139, 159, 179, 199, 219, 239, 259, 279, 299, 319, 339, 359, 379, 399],
    bottomSide: [380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399],
    //创建游戏盒子
    CreatGameBox: function() {
        //创建一个20*20的游戏盒子
        for (var i = 0; i < 400; i++) {
            $(".game-box").append($("<li></li>"));
        }
    }
}

//项目入口函数/主函数
$(function() {
    GameBox.CreatGameBox();
    //初始化小蛇
    Snack.CreatSnack();
    //初始化食物
    Food.CreatFood();
    //给屏幕上的控制按钮添加事件
    $("body").on("keydown", function(e) {
        //清楚按键默认事件
        event.preventDefault();
        //清楚计时器
        clearInterval(Snack.timeId);
        //定义临时变量，储存按键的方向
        var key = null;
        //判断按键，根据不同按键做出不同反应
        switch (e.keyCode) {
            case 38:
            case 87:
                key = 1;
                break;
            case 40:
            case 83:
                key = 2;
                break;
            case 65:
            case 37:
                key = 3;
                break;
            case 39:
            case 68:
                key = 4;
                break;
            case 32:
                Snack.pause = !Snack.pause; //空格键，暂停或者继续游戏
                break;
        }
        //检测蛇的新方向是否与前进方向相反，如果是，则走老方向，如果不是，走新方向
        if (key + Snack.direction != 3 && key + Snack.direction != 7) {
            console.log(key);
            Snack.direction = key;
        }

        // console.log(Snack.direction);
        Snack.snackMove();
    });
});